package com.um.jdy.app.shop.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.um.jdy.app.shop.entity.dto.*;
import com.um.jdy.app.shop.entity.vo.AftersaleDetailVO;
import com.um.jdy.app.shop.entity.vo.ShopAftersaleItemVO;
import com.um.jdy.app.shop.manager.ShopAftersaleManager;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * @author renjunwei
 * @date 2020/9/19 09:37
 */
@RestController
@RequestMapping("/api/v1/shop/aftersale/")
@SaCheckLogin
@Validated
@Api(tags = "商城售后")
public class ShopAftersaleController {

    @Autowired
    ShopAftersaleManager shopAftersaleManager;


    /**
     * 分页查询我的售后
     * @param dto
     * @return
     */
    @ApiOperation("分页查询我的售后")
    @GetMapping("page")
    public Response<Page<ShopAftersaleItemVO>> getMyCommentPage(@Valid @ModelAttribute AftersalePageDTO dto){
        return Response.success(shopAftersaleManager.getPage(dto));
    }

    /**
     * 查询售后详情
     * @param id
     * @return
     */
    @ApiOperation("查询售后详情")
    @GetMapping("{id}")
    public Response<AftersaleDetailVO> get(@ApiParam("售后ID") @PathVariable String id){
        return Response.success(shopAftersaleManager.get(id));
    }

    /**
     * 申请售后
     * @param dto
     * @return
     */
    @ApiOperation("申请售后")
    @PostMapping()
    public Response apply(@Valid @RequestBody AddAftersaleDTO dto){
        shopAftersaleManager.apply(dto, null);
        return Response.success();
    }

    /**
     * 修改申请售后
     * @param dto
     * @return
     */
    @ApiOperation("修改申请售后")
    @PutMapping("{id}")
    public Response updateApply(@ApiParam("售后ID") @PathVariable String id,
                                  @Valid @RequestBody AddAftersaleDTO dto){
        shopAftersaleManager.updateApply(id, dto);
        return Response.success();
    }

    /**
     * 取消售后
     * @param id
     * @return
     */
    @ApiOperation("取消售后")
    @PostMapping("cancel/{id}")
    public Response cancel(@ApiParam("售后ID") @PathVariable String id){
        shopAftersaleManager.cancel(id);
        return Response.success();
    }

    /**
     * 删除售后
     * @param id
     * @return
     */
    @ApiOperation("删除售后")
    @PostMapping("delete/{id}")
    public Response delete(@ApiParam("售后ID") @PathVariable String id){
        shopAftersaleManager.delete(id);
        return Response.success();
    }

    /**
     * 填写物流
     * @param dto
     * @return
     */
    @ApiOperation("填写物流")
    @PostMapping("delivery")
    public Response delivery(@Valid @RequestBody AftersaleDeliveryDTO dto){
        shopAftersaleManager.delivery(dto);
        return Response.success();
    }


    /**
     * 确认收货
     * @param id
     * @return
     */
    @ApiOperation("确认收货")
    @PostMapping("receive/{id}")
    public Response receive(@ApiParam("售后ID") @PathVariable String id){
        shopAftersaleManager.receive(id);
        return Response.success();
    }

    /**
     * 查询快递轨迹
     * @param id
     * @return
     */
    @ApiOperation("查询快递轨迹")
    @GetMapping("track/{id}")
    public Response getExpressTrack(@ApiParam("售后ID") @PathVariable String id){
        return Response.success(shopAftersaleManager.getExpressTrack(id, false));
    }

    /**
     * 查询退货快递轨迹
     * @param id
     * @return
     */
    @ApiOperation("查询退货快递轨迹")
    @GetMapping("return_track/{id}")
    public Response getReturnExpressTrack(@ApiParam("售后ID") @PathVariable String id){
        return Response.success(shopAftersaleManager.getExpressTrack(id, true));
    }

    /**
     * 查询售后动态
     * @param id
     * @return
     */
    @ApiOperation("查询售后动态")
    @GetMapping("dynamic/{id}")
    public Response getDynamicList(@ApiParam("售后ID") @PathVariable String id){
        return Response.success(shopAftersaleManager.getDynamicList(id));
    }

    /**
     * 查询售后原因
     * @return
     */
    @SaIgnore
    @ApiOperation("查询售后原因")
    @GetMapping("reasons")
    public Response getReasons(){
        return Response.success(shopAftersaleManager.getReasons());
    }

}
